﻿using Peanut.Common.Tools.Excel;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Reflection.Emit;

namespace TlToolBase
{
    /// <summary>
    /// 文件帮助
    /// </summary>
    public class FileHelp
    {
        /// <summary>
        /// 加载Excel表格
        /// </summary>
        /// <param name="path">地址</param>
        /// <returns></returns>
        public static OperationResults<Dictionary<string, System.Data.DataTable>> LoadExcel(string path)
        {
            OperationResults<Dictionary<string, System.Data.DataTable>> dst = new OperationResults<Dictionary<string, System.Data.DataTable>>();
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            var files = Directory.GetFiles(path);
            dst.Result = new Dictionary<string, DataTable>();
            foreach (var item in files)
            {
                var ext = Path.GetExtension(item);
                if (ext == ".xls" || ext == ".xlsx")
                {
                    Dictionary<string, System.Data.DataTable> tables = null;
                    try
                    {
                        tables = NpoiExcel.ExcelToDataTables(item);
                    }
                    catch (Exception ex)
                    {
                        dst.Success = false;
                        dst.Message = ex.Message + ex.InnerException?.Message;
                        break;
                    }

                    foreach (var dbs in tables)
                    {
                        dst.Result.TryAdd(dbs.Key, dbs.Value);
                    }
                }
            }
            dst.Success = true;
            return dst;
        }
        /// <summary>
        /// 获取图片路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static OperationResults<List<string>> LoadImg(string path)
        {
            OperationResults<List<string>> dst = new OperationResults<List<string>>();
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            var files = Directory.GetFiles(path);
            dst.Result = new List<string>();
            foreach (var item in files)
            {
                var ext = Path.GetExtension(item);
                if (ext == ".png" || ext == ".jpg")
                {
                    dst.Result.Add(item);
                }
            }
            dst.Success = true;
            return dst;
        }
    }
}